package warehouse;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
 * Manager class creates order objects
 * checks for the validity of input in the constructor
 * @author Apeiranthitis, Grassos, Moschos
 *
 */

public class Manager {

	//private Worker worker; 
	private OrderList allOrders;
	private ItemList allItems;

	/**
	 * Initializes the Manager class, creating two new objects: 
	 * One for the OrderList and another for the ItemList
	 */
	public Manager()
	{
		allOrders = new  OrderList();
		allItems=new ItemList();

	}

	/**
	 * main method creates a manager object an call run method
	 * @param args
	 */
	public static void main(String[] args) {
		Manager manager = new Manager();
		manager.run();
	}

	/**
	 * calls initialise() and process() methods
	 */
	public void run(){
		this.initialise();
		Item ms=new Item();
		Gui gui=new Gui(allOrders,allItems,ms);
		gui.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
		gui.setSize(800,600);
		gui.pack();
		gui.setVisible(true);
		
			

	}

	/**
	 * Reads Orders.txt, process each line, checks whether there is the same Order ID already exists
	 * checks if the order id is wrong and does not add the order in the order list,
	 * adds each Order`s detail to the OrderList.
	 * Additionally, it reads each line from Items text file, splits it appropriately, checks if
	 * id already exists or id is wrong and does not add the item in item list 
	 * and finally catches any FileNotFoundException might be appeared.
	 */
	public void initialise(){
		try {
			Scanner scanner = new Scanner (new File("Orders.txt"));
			while(scanner.hasNext()){  
				String inputLine = scanner.nextLine();
				Order order= new Order (inputLine);
				//check if the order id already exists
				if(!allOrders.checkEquality(order)){
					//check for wrong id if something was wrong in the order constructor
					if(!order.getOrderID().equals("Wrong")){
						//if everything is correct add to list
						allOrders.addDetails(order);
					}
				}
			}
			Scanner scanner2 = new Scanner (new File("Items.txt"));
			while(scanner2.hasNext()){  
				String inputLine = scanner2.nextLine();
				String parts1 [] = inputLine.split(",");
				String id=parts1[0].trim();
				String colour=parts1[1].trim();
				String type=parts1[2].trim();
				String buttons=parts1[3].trim();
				int buttonNum=Integer.parseInt(buttons);
				String price=parts1[4].trim();
				String quantity=parts1[5].trim();
				//create mouse item
				Mouse m= new Mouse (id,price,quantity,type,buttonNum,colour);
				//check if the item id already exists
				if (!allItems.keyExists(id)){
					//check for wrong id if something was wrong in the item constructor
					if (!m.getItemID().equals("Wrong")){
						//if everything is correct add to list with the item id as key value
						allItems.addDetails(m.getItemID(),m);
					}
				}
			}   

		}
		catch (FileNotFoundException e) {
			System.out.println("File has not been found. ");
		}


	}

	/**
	 *  Creates a new Worker class object and calls the run() method 
	 */

}

